<script setup lang="ts">
import DirectiveEditor from '@/views/domain/ngx_conf/directive/DirectiveEditor.vue'
import LocationEditor from '@/views/domain/ngx_conf/LocationEditor.vue'
import NgxConfigEditor from '@/views/domain/ngx_conf/NgxConfigEditor.vue'
import {useGettext} from 'vue3-gettext'
import domain from '@/api/domain'
import ngx from '@/api/ngx'
import {computed, provide, reactive, ref} from 'vue'
import {message} from 'ant-design-vue'
import {useRouter} from 'vue-router'

const {$gettext, interpolate} = useGettext()

const ngx_config = reactive({
    name: '',
    servers: [{
        directives: [],
        locations: []
    }]
})

const error = reactive({})

const current_step = ref(0)

const enabled = ref(true)

const auto_cert = ref(false)

const update = ref(0)

init()

function init() {
    domain.get_template().then(r => {
        Object.assign(ngx_config, r.tokenized)
    })
}

function save() {
    return ngx.build_config(ngx_config).then(r => {
        domain.save(ngx_config.name, {name: ngx_config.name, content: r.content, overwrite: true}).then(() => {
            message.success($gettext('Saved successfully'))

            domain.enable(ngx_config.name).then(() => {
                message.success($gettext('Enabled successfully'))
                window.scroll({top: 0, left: 0, behavior: 'smooth'})
            }).catch(r => {
                message.error(r.message ?? $gettext('Enable failed'), 5)
            })

        }).catch(r => {
            message.error(interpolate($gettext('Save error %{msg}'), {msg: $gettext(r.message) ?? ''}), 5)
        })
    })
}

const router = useRouter()

function goto_modify() {
    router.push('/domain/' + ngx_config.name)
}

function create_another() {
    router.go(0)
}

const has_server_name = computed(() => {
    const servers = ngx_config.servers
    for (const server_key in servers) {
        for (const k in servers[server_key].directives) {
            const v: any = servers[server_key].directives[k]
            if (v.directive === 'server_name' && v.params.trim() !== '') {
                return true
            }
        }
    }

    return false
})

provide('save_site_config', save)

async function next() {
    await save()
    current_step.value++
}
</script>

<template>
    <a-card :title="$gettext('Add Site')">
        <div class="domain-add-container">
            <a-steps :current="current_step" size="small">
                <a-step :title="$gettext('Base information')"/>
                <a-step :title="$gettext('Configure SSL')"/>
                <a-step :title="$gettext('Finished')"/>
            </a-steps>
            <template v-if="current_step===0">
                <a-form layout="vertical">
                    <a-form-item :label="$gettext('Configuration Name')">
                        <a-input v-model:value="ngx_config.name"/>
                    </a-form-item>
                </a-form>

                <directive-editor :ngx_directives="ngx_config.servers[0].directives"/>
                <br/>
                <location-editor :locations="ngx_config.servers[0].locations"/>
                <br/>
                <a-alert
                    v-if="!has_server_name"
                    :message="$gettext('Warning')"
                    type="warning"
                    show-icon
                >
                    <template #description>
                        <span v-translate>server_name parameter is required</span>
                    </template>
                </a-alert>
                <br/>
            </template>

            <template v-else-if="current_step===1">

                <ngx-config-editor
                    ref="ngx-config-editor"
                    :ngx_config="ngx_config"
                    v-model:auto_cert="auto_cert"
                    :enabled="enabled"
                />

                <br/>

            </template>

            <a-space v-if="current_step<2">
                <a-button
                    type="primary"
                    @click="next"
                    :disabled="!ngx_config.name||!has_server_name"
                >
                    <translate>Next</translate>
                </a-button>
            </a-space>
            <a-result
                v-else-if="current_step===2"
                status="success"
                :title="$gettext('Domain Config Created Successfully')"
            >
                <template #extra>
                    <a-button type="primary" @click="goto_modify">
                        <translate>Modify Config</translate>
                    </a-button>
                    <a-button @click="create_another">
                        <translate>Create Another</translate>
                    </a-button>
                </template>
            </a-result>

        </div>
    </a-card>
</template>

<style lang="less" scoped>
.ant-steps {
    padding: 10px 0 20px 0;
}

.domain-add-container {
    max-width: 800px;
    margin: 0 auto
}
</style>
